﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using WcfFlashRemoting.AmfObjects;

namespace WcfFlashRemoting.IO.Readers
{
    internal abstract class AmfBaseReader
    {
        protected BinaryReader reader;

        public AmfBaseReader(BinaryReader reader)
        {
            this.reader = reader;
        }

        public virtual void ReadMessage(AmfObjects.AmfMessage message)
        {

            // read headers
            ushort headerCount = reader.ReadUInt16();
            for (int i = 0; i < headerCount; i++)
            {
                message.Headers.Add(ReadHeader());
            }

            // read body
            ushort bodyCount = reader.ReadUInt16();
            for (int i = 0; i < bodyCount; i++)
            {
                AmfBody amfBody = ReadBody();
                if (amfBody != null)//not failed
                {
                    message.Body.Add(amfBody);
                }
            }
        }


        public abstract AmfHeader ReadHeader();
        public abstract AmfBody ReadBody();
    }
}
